package com.delaphone.flocash.web.action;

import org.jpos.gl.Currency;
import com.delaphone.flocash.model.FBanks;
import com.delaphone.flocash.service.AccountService;
import com.delaphone.flocash.service.BankService;
import com.delaphone.flocash.service.SystemService;
import com.delaphone.flocash.web.util.Validate;
import com.opensymphony.xwork2.Action;
import java.math.BigDecimal;
import java.util.List;

/**
 *
 * @author tamdx
 */
public class WithDrawAction extends BaseAction {

    private BankService bankService;
    private SystemService sysService;
    private AccountService accountService;
    private List<FBanks> listBank;
    private FBanks bank_;
    private List<Currency> currencies;
    private String bank_id;
    private String amount;
    private String currency;
    private String bankName;

    /**
     * Display form to withdraw
     * @return
     */
    public String step1() {
        if (user == null) {
            return Action.LOGIN;
        }
        return Action.SUCCESS;
    }

    /**
     * confirm info to withdraw
     * @return
     */
    public String step2() {
        if (user == null) {
            return Action.LOGIN;
        }

        validateFields();
        if (getActionErrors().size() > 0) {
            return Action.INPUT;
        } else {
            bank_ = bankService.getBankById(Integer.valueOf(bank_id));
            setBankName(bank_.getBankName());

            return Action.SUCCESS;
        }
    }

    /**
     * execute with draw
     * @return
     */
    public String step3() {
        if (user == null) {
            return LOGIN;
        }
        logger.error("--------------->Amount:" +getAmount());
        BigDecimal amt = new BigDecimal(getAmount());
        logger.error("--------------->Amount:" +amt);
        bank_ = bankService.getBankById(Integer.valueOf(bank_id));
        boolean check = accountService.withdrawCash(user.getAccount(), amt, bank_.getId(), getCurrency(), "");
        if (check) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    public String step4() {
        return Action.SUCCESS;
    }

    public FBanks getBank_() {
        return bank_;
    }

    public void setBank_(FBanks bank_) {
        this.bank_ = bank_;
    }

    public BankService getBankService() {
        return bankService;
    }

    public void setBankService(BankService bankService) {
        this.bankService = bankService;
    }

    public List<FBanks> getListBank() {
        listBank = bankService.getListBankByUserId(user.getId());
        return listBank;
    }

    public void setListBank(List<FBanks> listBank) {
        this.listBank = listBank;
    }

    public SystemService getSysService() {
        return sysService;
    }

    public void setSysService(SystemService sysService) {
        this.sysService = sysService;
    }

    public List<Currency> getCurrencies() {
        currencies = sysService.getAllCurrencies();
        return currencies;
    }

    public void setCurrencies(List<Currency> currencies) {
        this.currencies = currencies;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getBank_id() {
        return bank_id;
    }

    public void setBank_id(String bank_id) {
        this.bank_id = bank_id;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
    }

    public AccountService getAccountService() {
        return accountService;
    }

    public void setAccountService(AccountService accountService) {
        this.accountService = accountService;
    }

    public void validateFields() {
        if (Validate.isEmptyString(bank_id)) {
            addActionError("Choose a bank to with draw");
        } else if (Validate.isEmptyString(amount)) {
            addActionError("Enter a number to withdraw");
        } else if (!Validate.isNumber(amount)) {
            addActionError("Enter correct format");
        } else {
            BigDecimal amt = new BigDecimal(amount);
            BigDecimal currBalance = accountService.getAccountBalance(user.getAccount());
            System.out.println("---------------->Balance:" + currBalance);
            if (currBalance.compareTo(amt) < 0) {
                addActionError("Not enough balance to withdraw!");
                return;
            }
        }
    }
}
